package top.suven.base.util.createcode;

import org.apache.commons.lang.StringUtils;

public class Column {

	private String tableName;
	private String className;

	private String fieldName;
	
	private String dbName;
	
	private String ucName; //大写开头
	private String lcName; //小写开发;
	
	private String getFileName;
	
	private String setFileName;

	private String desc;

	private String type;

	private int length;

	private boolean nullable;

	private String id;

	private boolean unique;

	public String getTbaleName() {
//		String nm = 
//				append(nameLower).append(".get")
//				.append(column.getUcName())
		
		return tableName;
	}

	public void setClassName(String name) {
		this.className = name;
	}

	public boolean isUnique() {
		return unique;
	}

	public void setUnique(boolean unique) {
		this.unique = unique;
	}

	public String getTableName() {
		return tableName;
	}

	public String getId() {
		return id;
	}

	public long getMin() {
		return 0;
	}

	public long getMax() {
		return Long.MAX_VALUE;
	}
	public String getMaxStr() {
		return Long.MAX_VALUE +"";
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setTableName(String table) {
		this.tableName = table;
	}

	public String getDesc() {
		if (!StringUtils.isEmpty(desc)) {
			if (this.isEnum()) {
				String[] values = getSplitDesc();
				if (split(values[0]).length > 1) {
					return getMethodName();
				} else {
					return values[0];
				}
			} else {
				return desc;
			}
		} else {
			return getFieldName();
		}
	}

	public String[] split(String value) {
		String[] values = value.split("：");
		if (values.length == 1) {
			values = value.split(":");
		}
		return values;
	}

	public String[] getSplitDesc() {
		if (StringUtils.isEmpty(desc)) {
			return new String[] { "" };
		}
		String[] values = desc.split("\r\n");
		if (values.length == 1) {
			values = desc.split("\n");
		}
		return values;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getFieldName() {
		if (fieldName.toLowerCase().equals("id")) {
			return getId();
		} else {
			return fieldName;
		}
	}

	public void setFieldName(String fieldName) {
	    String r= "";
	    for (char c : fieldName.toCharArray()) {
		    if (Character.isUpperCase(c)) {
			r += ("_" + c).toLowerCase();
		    } else {
			r += ("" + c);
		    }
	   }
	   this.ucName =Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
	   this.lcName = Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1);
	   this.dbName = r;
	   this.fieldName = fieldName;
	   
	  
	}

	public String getType() {
		if (isEnum()) {
			return type.equals("String") ? "String" : "int";
		} else {
			return type;
		}

	}

	public int getLength() {
		return length;
	}
	public String getLengthStr() {
		return  new String(length+"");
	}

	public void setLength(int length) {
		this.length = length;
	}

	public boolean isRadio() {
		if (isEnum()) {
			String[] enums = getSplitDesc();
			String[] enumEntry = split(enums[0]);
			if (enumEntry.length == 1) {
				return enums.length == 3;
			} else {
				return enums.length == 2;
			}
		} else {
			return false;
		}
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMethodName() {
		return getFieldName().substring(0, 1).toUpperCase() + getFieldName().substring(1);
	}
	
	public String getEnumFieldName(){
		return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
	}

	public boolean isEnum() {
		if (StringUtils.isEmpty(desc)) {
			return false;
		} else {
			return getSplitDesc().length > 1 && split(getSplitDesc()[1]).length > 1;
		}
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	public String getValidate() {
		String msg = "";
		if (!isNullable()) {
			msg = "required='true'";
			msg = String.format(msg, getDesc());
		}
		if (getType().equals("String")) {
			long min = getMin() > 0 ? getMin() : 0;
			long max = getLength();
			msg += " class='easyui-validatebox' validType='length[%d,%d]''";
			msg = String.format(msg, min, max, getDesc(), min, max);
		} else if (getType().equals("Long") || getType().equals("Integer") || getType().equals("int")) {
			long min = getMin();
			long max = getMax();
			msg += " class='easyui-numberbox' min='%d' max='%d'";
			msg = String.format(msg, min, max, getDesc(), min, max);
		}

		return msg;
	}

	/**
	 * @return the dbName
	 */
	public String getDbName() {
	    return dbName;
	}
	/**大写开始)*/
	public String getUcName() {
	    return ucName;
	}
	/**小写开始)*/
	public String getLcName() {
	    return lcName;
	}


	public String getGetFileName() {
		String lcn = Character.toLowerCase(className.charAt(0)) + className.substring(1);
		getFileName = lcn+".get"+getUcName()+ "()";
		return getFileName;
	}



	public String getSetFileName() {
		String lcn = Character.toLowerCase(className.charAt(0)) + className.substring(1);
		setFileName = lcn+".set"+getUcName()+" (" + fieldName +")";
		return setFileName;
	}
	
	public String getDbFileName(){
		StringBuffer vo = new StringBuffer();
		String columtype = Character.toUpperCase(this.getType().charAt(0))+this.getType().substring(1);
		String setType = columtype;
		if(this.getType().equals("Integer")){
			setType = "Int";
		}if(this.getType().equals("Date") ){
			setType = "Timestamp";
		}
//		if(this.getDbName().equals("enable_status")){
//			vo.append("\n \t\t\tString enableStatus = ").append("rs.getString(\"enable_status\");");
//			vo.append("\n \t\t\tvo.setEnableStatus(StringUtils.isBlank(enableStatus)?SysDefineEnumType.ENABLE_STATUS_DISABLE.getIndex():Integer.parseInt(enableStatus));");
//		}else if(this.getDbName().equals("status")){
//			vo.append("\n \t\t\tString status = ").append("rs.getString(\"status\");");
//			vo.append("\n \t\t\tvo.setStatus(StringUtils.isBlank(status)?SysDefineEnumType.STATUS_NO.getIndex():Integer.parseInt(status));");
//		}else if(this.getDbName().endsWith("status")){
//			vo.append("\n \t\t\tString status = ").append("rs.getString(\"status\");");
//			vo.append("\n \t\t\tvo.set").append(this.getUcName()).append("(?SysDefineEnumType.STATUS_NO.getIndex():Integer.parseInt(status))");
//		}
//		else
		{
			vo.append("\n \t\t\tvo.set").append(this.getUcName()).append("(rs.get").append(setType);
			vo.append("(\"" + this.getDbName() + "\")").append(");");
		}
		return vo.toString();
	}

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }
}
